<template>
  <div class="main">
    <div class="left">
      <div class="title">平台帮助</div>
      <ul class="ul">
        <li>
          <router-link to="/"> CA办理指南</router-link>
        </li>
        <li>
          <router-link to="/"> 如何办理数字证书</router-link>
        </li>
        <li>
          <router-link to="/"> 投标文件制作软件中的评审资料需同步信.. </router-link>
        </li>
        <li>
          <router-link to="/"> CA办理指南</router-link>
        </li>
        <li>
          <router-link to="/"> 如何办理数字证书</router-link>
        </li>
        <li>
          <router-link to="/"> 投标文件制作软件中的评审资料需同步信.. </router-link>
        </li>
      </ul>
      <div class="down">
        <div><router-link to="/">手册下载</router-link></div>
        <div><router-link to="/">驱动下载</router-link></div>
        <div><router-link to="/">投标文件制作软件下载</router-link></div>
      </div>
    </div>
    <a-form-model class="user-layout-login" style="flex:1;" @keyup.enter.native="handleSubmit">
      <login-account
        ref="alogin"
        v-if="false"
        @validateFail="validateFail"
        @success="requestSuccess"
        @fail="requestFailed"
      ></login-account>
      <a-tabs
        v-if="true"
        :activeKey="customActiveKey"
        :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
        @change="handleTabClick"
      >
        <a-tab-pane key="tab1" tab="账号密码登录">
          <login-account
            ref="alogin"
            @validateFail="validateFail"
            @success="requestSuccess"
            @fail="requestFailed"
          ></login-account>
        </a-tab-pane>

        <a-tab-pane key="tab2" tab="CA登录" v-if="true">
          <login-phone
            ref="plogin"
            @validateFail="validateFail"
            @success="requestSuccess"
            @fail="requestFailed"
          ></login-phone>
        </a-tab-pane>
      </a-tabs>

      <a-form-model-item>
        <a-checkbox @change="handleRememberMeChange" default-checked>自动登录</a-checkbox>
        <!--  <router-link :to="{ name: 'alteration'}" class="forge-password" style="float: right;">
          忘记密码
        </router-link>-->
        <router-link :to="{ name: 'register' }" class="forge-password" style="float: right;margin-right: 10px">
          注册账户
        </router-link>
      </a-form-model-item>
      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="loginBtn"
          @click.stop.prevent="handleSubmit"
          :disabled="loginBtn"
          >确定
        </a-button>
      </a-form-item>
    </a-form-model>

    <two-step-captcha
      v-if="requiredTwoStepCaptcha"
      :visible="stepCaptchaVisible"
      @success="stepCaptchaSuccess"
      @cancel="stepCaptchaCancel"
    ></two-step-captcha>
    <!-- <login-select-tenant ref="loginSelect" @success="loginSelectOk"></login-select-tenant> -->
    <login-select2 ref="loginSelect" @success="loginSelectOk"></login-select2>
    <third-login ref="thirdLogin" v-if="false"></third-login>
  </div>
</template>
<script>
import Vue from 'vue'
import { ACCESS_TOKEN, ENCRYPTED_STRING } from '@/store/mutation-types'
import ThirdLogin from './third/ThirdLogin'
import LoginSelectTenant from './LoginSelectTenant'
import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
import { getEncryptedString } from '@/utils/encryption/aesEncrypt'
import { timeFix } from '@/utils/util'

import LoginAccount from './LoginAccount'
import LoginPhone from './LoginPhone'
import { mapActions, mapGetters } from 'vuex'
import Register3 from './register/register3.vue'
import LoginSelect2 from './loginSelect2.vue'

export default {
  components: {
    LoginSelectTenant,
    TwoStepCaptcha,
    ThirdLogin,
    LoginAccount,
    LoginPhone,
    Register3,
    LoginSelect2,
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  data() {
    return {
      showregister3: false,
      customActiveKey: 'tab1',
      rememberMe: true,
      loginBtn: false,
      requiredTwoStepCaptcha: false,
      stepCaptchaVisible: false,
      encryptedString: {
        key: '',
        iv: '',
      },
    }
  },
  created() {
    Vue.ls.remove(ACCESS_TOKEN)
    this.getRouterData()
    this.rememberMe = true
  },
  methods: {
    ...mapActions(['loginCa']),
    handleTabClick(key) {
      this.customActiveKey = key
    },
    handleRememberMeChange(e) {
      this.rememberMe = e.target.checked
    },
    /**跳转到登录页面的参数-账号获取*/
    getRouterData() {
      this.$nextTick(() => {
        let temp = this.$route.params.username || this.$route.query.username || ''
        if (temp) {
          this.$refs.alogin.acceptUsername(temp)
        }
      })
    },

    //登录
    handleSubmit() {
      this.loginBtn = true
      if (this.customActiveKey === 'tab1') {
        // 使用账户密码登录
        this.$refs.alogin.handleLogin(this.rememberMe)
      } else {
        //CA登录
        this.caLogin()
      }
    },
    caLogin() {
      var that = this
      let signdata1 = _P7SignData() //签名数据
      //上线后删掉签名 上边方法放开
      // var signdata1 = 'MIIETQYKKoEcz1UGAQQCAqCCBD0wggQ5AgEBMQ4wDAYIKoEcz1UBgxEFADAYBgoqgRzPVQYBBAIBoAoECHRlc3RkYXRhoIIDAjCCAv4wggKioAMCAQICBAINShwwDAYIKoEcz1UBg3UFADCBijELMAkGA1UEBhMCQ04xDjAMBgNVBAgMBUhlTmFuMRIwEAYDVQQHDAlaaGVuZ1pob3UxNzA1BgNVBAoMLkhlTmFuIFByb3ZpbmNlIEluZm9ybWF0aW9uIERldmVsb3BtZW50IENvLiBMdGQxDzANBgNVBAsMBkhOWEFDQTENMAsGA1UEAwwEWEFDQTAeFw0yMjA1MjAwODU3MzJaFw0yMzA1MjAwODU3MzJaMIHIMQswCQYDVQQGEwJDTjESMBAGA1UECAwJ5rKz5Y2X55yBMRIwEAYDVQQHDAnpg5Hlt57luIIxKjAoBgNVBAoMIeays+WNl+ecgeWFrOWFsei1hOa6kOS6pOaYk+S4reW/gzEnMCUGA1UECwwe5rKz5Y2X6Lev5Y+L5bu65p2Q5pyJ6ZmQ5YWs5Y+4MR8wHQYJKoZIhvcNAQkBFhA0NjI5MTI1NzFAcXEuY29tMRswGQYDVQQDDBI5MTQxMDE4Mk1BNDRZQ1A2NFQwWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAATSRrlovmWoz75S63fVrHDpEQl90vFL+k/ZDgwsoo92JPfJX4CuLKEBYNYVktdi7BGGIBVeQtK+pyTJVLh80egFo4GzMIGwMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEFBQcDATAfBgNVHSMEGDAWgBRs8004Sr8JshO2b6a8S1ySOIFaWzA3BgNVHR8EMDAuMCygKqAohiZodHRwOi8vY3JsLmhueGFjYS5jb20vY3JsL3hhY2Ffc20yLmNybDALBgNVHQ8EBAMCBsAwHQYDVR0OBBYEFB129NXAVFciOvmq7rPqodVG9Rq1MAkGA1UdEwQCMAAwDAYIKoEcz1UBg3UFAANIADBFAiBDzNuqPoeSI/elC+CkoWv+T+WBHW9QUrwP7ct53AqOmgIhAI/FNjK6nB/QozaYenWJdMsunlGmQfJ8UcBhSBMYcQ7CMYIBAjCB/wIBATCBkzCBijELMAkGA1UEBhMCQ04xDjAMBgNVBAgMBUhlTmFuMRIwEAYDVQQHDAlaaGVuZ1pob3UxNzA1BgNVBAoMLkhlTmFuIFByb3ZpbmNlIEluZm9ybWF0aW9uIERldmVsb3BtZW50IENvLiBMdGQxDzANBgNVBAsMBkhOWEFDQTENMAsGA1UEAwwEWEFDQQIEAg1KHDAMBggqgRzPVQGDEQUAMA0GCSqBHM9VAYItAQUABEcwRQIgZiZW+qndoAGGcFYo9i2Bl7sJ6FRDX6CIQLVGGReKGx8CIQDEAG+TBvj//YouZS6kohhjTv81XG/dM6XjCmO7NrRMJQ==';
      if (signdata1 == '') {
        this.loginBtn = false
        this.$notification.error({
          message: 'CA登录失败',
          description: '请下载安装CA驱动并插入CA证书助手登录'
        })
        return
      }
      $.ajax({
        type: 'GET',
        data: { signData: signdata1 },
        contentType: 'application/json',
        dataType: 'json',
        url: process.env.VUE_APP_API_BASE_URL + '/api/ca/caLogin',
        success: data => {
          this.loginCa(data).then(_ => {
            if (data.code == 200) {
              this.requestSuccess(data)
            }
          }).catch(data=>{
            console.log(data)
            this.$notification['error']({
              message: '提示信息',
              description: 'CA登录失败！' + data.message,
            })
            this.loginBtn = false
          })
          /*if (data.success &&  data.code == 200) {
            console.info(data.result.checkResult);
            if(data.result.checkResult == '1'){
              sessionStorage.setItem("token",data.result.token);
              sessionStorage.setItem("companyInfo",JSON.stringify(data.result.companyInfo));
              that.$emit('success', data.result)
            } else {
              that.$notification['error']({
                message: '提示信息',
                description: 'CA登录失败！' + data.result.message,
              })
            }
            // 回调
          } else {
            // 其他状态
            that.$notification['error']({
              message: '提示信息',
              description: 'CA登录失败！' + data.message,
            })
          }*/
        },
        error: function(msg1,msg2,msg3) {
          console.log(msg1)
          console.log(msg2)
          console.log(msg3)
          that.$notification['error']({
            message: '提示信息',
            description: 'CA登录失败！' + msg,
          })
        },
      })
    },
    // 校验失败
    validateFail() {
      this.loginBtn = false
    },
    // 登录后台成功
    requestSuccess(loginResult) {
      this.$refs.loginSelect.show(loginResult)
    },
    //登录后台失败
    requestFailed(err) {
      let description = ((err.response || {}).data || {}).message || err.message || '请求出现错误，请稍后再试'
      /*this.$notification['error']({
        message: '登录失败',
        description: description,
        duration: 4,
      })*/
      //账户密码登录错误后更新验证码
      if (this.customActiveKey === 'tab1' && description.indexOf('密码错误') > 0) {
        this.$refs.alogin.handleChangeCheckCode()
      }
      this.loginBtn = false
    },
    loginSelectOk(v, selectedRole) {
      if (!v.roleList || !v.roleList.length) {
        this.loginSuccess()
        return
      }

      let role = selectedRole ?? v.roleList[0].roleCode

      if (role === 'AGENCY') {
        if (this.userInfo?.userSource == 4 && this.userInfo?.isAgencyRegister == 1) {
          this.$router.push({ path: '/userInfo-legal?redirect=/userInfo-legal' }).catch(() => {})
        } else {
          this.loginSuccess()
        }
      } else {
        if (this.userInfo?.userSource === 4 && this.userInfo?.isRegister === 1) {
          this.$router.push({ path: '/userInfo?redirect=/userInfo' }).catch(() => {})
        } else {
          this.loginSuccess()
        }
      }
    },
    //登录成功
    loginSuccess() {
      this.$router.push({ path: '/dashboard/analysis' }).catch(() => {
        // console.log('登录跳转首页出错,这个错误从哪里来的')
      })
      this.$notification.success({
        message: '欢迎',
        description: `${timeFix()}，欢迎回来`,
      })
    },

    stepCaptchaSuccess() {
      this.loginSuccess()
    },
    stepCaptchaCancel() {
      this.Logout().then(() => {
        this.loginBtn = false
        this.stepCaptchaVisible = false
      })
    },
    //获取密码加密规则
    getEncrypte() {
      var encryptedString = Vue.ls.get(ENCRYPTED_STRING)
      if (encryptedString == null) {
        getEncryptedString().then(data => {
          this.encryptedString = data
        })
      } else {
        this.encryptedString = encryptedString
      }
    },
  },
}
</script>
<style lang="less" scoped>
.main {
  display: flex;
  padding: 26px;
  width: 800px;
  background-color: #fff;
}
.left {
  width: 328px;
  font-size: 14px;
  border-right: 1px solid rgba(221, 221, 221, 1);
  margin-right: 45px;
  .title {
    font-size: 18px;
    color: rgba(174, 46, 51, 1);
  }
  ul {
    padding: 18px;
  }
  li {
    line-height: 1.5;

    white-space: nowrap; /* 防止文本换行 */

    text-overflow: ellipsis; /* 溢出部分用省略号表示 */
  }
  .down {
    div a {
      color: #fff;
      background-color: rgba(174, 46, 51, 1);
      padding: 5px 10px;
      display: inline-flex;
      margin-top: 5px;
    }
  }
}
.user-layout-login {
  label {
    font-size: 14px;
  }
  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
<style>
.valid-error .ant-select-selection__placeholder {
  color: #f5222d;
}
</style>
